package hedron.db;

import hedron.Application;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.UUID;
import java.util.logging.Level;

public class IDManager {
	private final DBManager DB;

	public IDManager(DBManager dbManager) {
		this.DB = dbManager;
	}

	public long getNewID() {
		long result = 0;
		Connection conn = DB.getConnection();
		if (conn != null) {
			try {
				ResultSet rs = DB.query(conn, "SELECT NEXT VALUE FOR HGLOBAL_SEQ");
				if (DB.next(rs)) {
					result = DB.getLong(rs, 1, 0);
				} else {
					Application.log.log(Level.OFF, "Cannot generate unique ID");
				}
				DB.close(rs);
			} finally {
				DB.closeConnection(conn);
			}
		}
		return result;
	}

	public String getUID() {
		return UUID.randomUUID().toString();
	}
}
